package com.hdoit.ares.web.home.controllers;


import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.wechat.WechatManager;
import com.tennetcn.web.cbase.AuthPassport;

/** 
 * @author      chenghuan
 * @email       79763939@qq.com
 * @createtime  2016年5月5日 上午11:40:25
 * @comment 
 */
@Controller
@RequestMapping(value = "/")
@AuthPassport
public class IndexController extends BaseController{
	
	@Override
	public String getRoutePath() {
		return "home/";
	}
	
	//全局请求变量
	private final String indexViewName="index";
	private final String mainViewName="main";
	private final String shareViewName="share";
	
	@RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String index(Model model){
		return view(indexViewName);
	}
	
	@RequestMapping(value = mainViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String main(Model model){
		return view(mainViewName);
	}
	
	@RequestMapping(value = shareViewName+"/{id}", method = { RequestMethod.GET,RequestMethod.POST })
	public String share(Model model,@PathVariable String id){
		WxMpService wxMpService=WechatManager.getInstance().getWxMpService();
		try {
			WxMpQrCodeTicket wxMpQrCodeTicket=wxMpService.getQrcodeService().qrCodeCreateTmpTicket(id, 60*5);
			model.addAttribute("qrUrl", wxMpQrCodeTicket.getUrl());
		} catch (WxErrorException e) {
			e.printStackTrace();
		}
		model.addAttribute("id", id);
		return view(shareViewName);
	}
}
